<div class="box box-primary" id="control">
    <div class="box-header">
        <div class="col-sm-10" style="padding-top:8px;">
            <i class="fa fa-floppy-o">&nbsp;&nbsp;</i>
            <h1 class="box-title" style="font-weight:bold;" id="id_h1_page_title"></h1>
        </div>
        
        <div class="col-sm-2" style="font-size:16px;padding-top:8px;">
            <a id="id_a_official_doc" target="_blank" href="https://www.ventoy.net/en/plugin_automemdisk.html"><span class="fa fa-link"></span><span id="id_span_official_doc">官网文档</span></a>
        </div>
    </div>
    <legend></legend>
    
    <div class="box-body">
      <div class="nav-tabs-custom">
        <ul class="nav nav-tabs" id="id_tab_auto_memdisk">
            <li class=""><a href="#tab_0" data-toggle="tab" aria-expanded="false" style="font-weight:bold" >auto_memdisk</a></li>
            <li class=""><a href="#tab_1" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">auto_memdisk_legacy</a></li>
            <li class=""><a href="#tab_2" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">auto_memdisk_uefi</a></li>
            <li class=""><a href="#tab_3" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">auto_memdisk_ia32</a></li>
            <li class=""><a href="#tab_4" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">auto_memdisk_aa64</a></li>
            <li class=""><a href="#tab_5" data-toggle="tab" aria-expanded="false"  style="font-weight:bold">auto_memdisk_mips</a></li>
        </ul>
      </div>

      <table id="id_auto_memdisk_tbl" class="table table-bordered">
        <thead>
          <tr>
            <th style="width: 5%;">#</th>
            <th id="id_th_memdisk_path" style="width: 60%;"></th>
            <th id="id_th_status" style="width: 10%;"></th>
            <th id="id_th_operation" style="width: 10%;"></th>
          </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    </div>
</div>
<script type="text/javascript">
    
  function VtoyPageLanguageChange(newlang) {
    VtoyCommonChangeLanguage(newlang);
    $('h1[id=id_h1_page_title]').text(g_vtoy_cur_language.STR_PLUG_AUTO_MEMDISK);

    $("span[id=id_span_file_exist]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_EXIST);
    });
    $("span[id=id_span_file_nonexist]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_NONEXIST);
    });
    $("span[id=id_span_file_fuzzy]").each(function(){
        $(this).text(g_vtoy_cur_language.STR_FILE_FUZZY);
    });

    if (newlang === 'en') {
      $('#id_th_memdisk_path').text('Absolute Path');
    } else {
      $('#id_th_memdisk_path').text('绝对路径');
      
    }
  }
  
  function FillMemdiskTable(data) {
    var addbtn = ventoy_get_xslg_addbtn('MemdiskAddBtn');
    var delbtn = ventoy_get_xslg_delbtn('MemdiskDelBtn');

    var td1, td2, td3, td4;
    var $tbl = $("#id_auto_memdisk_tbl tbody");
    $tbl.empty();

    for (var i = 0; i < data.length; i++) {
      var $tr;
      td1 = '<td>' + (i + 1) + '</td>';
      td2 = '<td>' + data[i].path + '</td>';
      td3 = '<td>' + ventoy_get_status_line(0, data[i].valid) + '</td>';
      td4 = '<td>' + delbtn + '</td>';

      $tr = $('<tr>' + td1 + td2 + td3 + td4 + '</tr>');

      $tr.data('path', data[i].path);
      $tr.data('index', i);
      $tbl.append($tr);
    }

    $tbl.append('<tr><td></td><td></td><td></td><td>' + addbtn + '</td></tr>');
  }

  function VtoyFillCurrentPageItem(data) {
    FillMemdiskTable(data);
  }


  function OnClickMultiModeTab() {
    var href = $(this).attr('href');
    var index = parseInt(href.substr(5, 1));

    if (index < 0 || index >= g_vtoy_data_default_index || current_tab_index === index) {
      return;
    }
    
    current_tab_index = index;
    VtoyFillCurrentPageItem(m_data_memdisk[index]);
  }

  //Main process
  var m_data_memdisk;
  var current_tab_index = 0;
  callVtoySync({method : 'get_auto_memdisk'}, function(data) {
    m_data_memdisk = data;
  });
  
  function OnAddAutoMemdisk(root, valid, extra) {
    var list = m_data_memdisk[current_tab_index];
    var data = {
      "path": root.substr(g_current_dir.length),
      "valid": valid
    };

    for (var i = 0; i < list.length; i++) {
      if (list[i].path === data.path) {
        Message.error(g_vtoy_cur_language.STR_DUPLICATE_PATH);
        return;
      }
    }

    callVtoy({
      method : 'auto_memdisk_add',
      index: current_tab_index,
      path: data.path,
    }, function(e) {
      list.push(data);
      FillMemdiskTable(list);
      Message.success(g_vtoy_cur_language.STR_SAVE_SUCCESS);
    });
  }

  $("#id_auto_memdisk_tbl").on('click', '.MemdiskAddBtn', function() {
    var tip1 = (g_current_os === 'windows') ? '\\ISO\\Windows11.iso' : "/ISO/Ubuntu-20.04-desktop-amd64.iso";
    var tip2 = (g_current_os === 'windows') ? '\\ISO\\Windows**.iso' : "/ISO/Ubuntu-*****-desktop-amd64.iso";
    var para = {
        "title": g_vtoy_cur_language.STR_ADD_FILE_TO_LIST,
        "fuzzy": 1,
        "tip1": g_current_dir + tip1,
        "tip2": g_current_dir + tip2,
        "tip3": '',
        "extra": false,
        "extra_title": ''
    };
    VtoySelectFilePath(OnAddAutoMemdisk, para);
  });

  $("#id_auto_memdisk_tbl").on('click', '.MemdiskDelBtn', function() {
    var $tr = $(this).closest('tr');
		var path = $tr.data('path');
    var index = $tr.data('index');

    callVtoySync({
        method : 'auto_memdisk_del',
        index: current_tab_index,
        path: path
    }, function(data) {
      m_data_memdisk[current_tab_index].splice(index, 1);
      FillMemdiskTable(m_data_memdisk[current_tab_index]);
      Message.success(g_vtoy_cur_language.STR_SAVE_SUCCESS);
    });
  });

  $('#id_tab_auto_memdisk a[href="#tab_0"]').click(OnClickMultiModeTab);
  $('#id_tab_auto_memdisk a[href="#tab_1"]').click(OnClickMultiModeTab);
  $('#id_tab_auto_memdisk a[href="#tab_2"]').click(OnClickMultiModeTab);
  $('#id_tab_auto_memdisk a[href="#tab_3"]').click(OnClickMultiModeTab);
  $('#id_tab_auto_memdisk a[href="#tab_4"]').click(OnClickMultiModeTab);
  $('#id_tab_auto_memdisk a[href="#tab_5"]').click(OnClickMultiModeTab);

  $('#id_tab_auto_memdisk a[href="#tab_0"]').tab('show');
  VtoyFillCurrentPageItem(m_data_memdisk[0]);
  VtoyPageLanguageChange(g_current_language);

</script>
